package redis

import (
	"base-gin/app/config"
	"fmt"
	"github.com/go-redis/redis"
	"go.uber.org/zap"
)

var Conn *redis.Client

// Init 初始化连接
func init() {
	Conn = redis.NewClient(&redis.Options{
		Addr:         fmt.Sprintf("%s:%d", config.RedisConfig.Host, config.RedisConfig.Port),
		Password:     config.RedisConfig.Password, // no password set
		DB:           config.RedisConfig.DB,       // use default DB
		PoolSize:     config.RedisConfig.PoolSize,
		MinIdleConns: config.RedisConfig.MinIdleConns,
	})
	_, err := Conn.Ping().Result()
	if err != nil {
		panic(fmt.Errorf("redis连接失败：%s", err))
	}
	zap.L().Info("redis加载成功！")
}

func Close() {
	_ = Conn.Close()
}
